{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "db3cdbec",
   "metadata": {},
   "source": [
    "-----------\n",
    "* 记录人： 郑秋珍\n",
    "* 时间： 2021年12月2日\n",
    "* week14\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c30d228",
   "metadata": {},
   "source": [
    "#  函数\n",
    "#  函数的定义\n",
    "> 1.数学：f(x) = ax + b\n",
    "> 2.python def fib(x）：\n",
    "> 3.编程思维 VS 数学思维\n",
    ">> 1. 函数可以被定义名称，定义的关键字  <font style=\"color:red\">**def**</font>\n",
    ">> 2. 数学思维是写在同一行 （定义函数 和 运算公式），面向对象编程中 函数写多行，定义函数为单独一行\n",
    ">> 3. 数学函数一般没有注释，而编程过程中，一般需要对函数进行注释描述（docstring,课本P151)\n",
    ">> 4. 数学中：ax + b 是计算公式，而编程中，我们将函数变为**函数体**，函数体要有一定的**缩进**\n",
    "\n",
    "#  函数的进阶\n",
    "## 关于函数的更多定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "26f74c60",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 斐波那切数列： 0 1 1 2 3 5 8 13 21 34 ...\n",
    "def fib(x):\n",
    "    \"\"\"列出指定x以内的斐波那切数列\"\"\"\n",
    "    a,b = 0,1\n",
    "    while a < x :\n",
    "        print(a,end=\" \")\n",
    "        a,b = b, a+b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7b0cb2cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 55 89 "
     ]
    }
   ],
   "source": [
    "fib(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "63aabd9a",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character in identifier (<ipython-input-19-1d485d2654bf>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-19-1d485d2654bf>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    def hello(name)：\u001b[0m\n\u001b[1;37m                   ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character in identifier\n"
     ]
    }
   ],
   "source": [
    "def hello(name)：\n",
    "    \"\"\"显示问候语\"\"\"\n",
    "    print(\"你好，%s同学~%(name)\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bbb38fc",
   "metadata": {},
   "source": [
    "> 2. 调用函数/使用函数\n",
    ">> A.函数\n",
    ">> B.参数可以不存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "45b89fd0",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'hello' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-20-0c47027b3e77>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhello\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Lihua\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'hello' is not defined"
     ]
    }
   ],
   "source": [
    "hello(\"Lihua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2279290",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 回顾与思考\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e27a532c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# sleep()需要给定一个时间（int)参数\n",
    "time fib(x):\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1280d24",
   "metadata": {},
   "source": [
    "> 3.函数的优势\n",
    ">> 1.可以重复使用相同的功能，避免重复造轮子\n",
    ">> 2.使用者可以不了解函数本身，只需要清楚如何使用即可\n",
    ">> 3. 使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a24e92a8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ff41dd0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "18f54b18",
   "metadata": {},
   "source": [
    "# 函数的参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bde51b1",
   "metadata": {},
   "source": [
    "> 1.形式参数（形参）\n",
    ">> 1. 在**定义**函数的时候定义的参数\n",
    ">> 2. 形式参数不占用内存\n",
    "\n",
    "> 2. 实际参数（实参）\n",
    ">> 1. 在**调用**函数的时候传递的参数\n",
    ">> 2. 调用函数的时候，形式参数分配空间，传递实际参数，执行函数内容 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9e02a9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# k=课本函数实际案例\n",
    "def search4vowels(word):\n",
    "    \"\"\"显示文本中出现的元音字母有哪些\"\"\"\n",
    "    vowels = set(\"aeiou\")\n",
    "    word = set(word)\n",
    "    print(vowels & word)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd1b262e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "626ee2de",
   "metadata": {},
   "source": [
    "* 在多个参数\n",
    "\n",
    "> 1. 位置参数\n",
    ">> 1. 在形式参数的定义中，有一定的先后顺序\n",
    ">> 2. 在调用函数时，要考虑形参的先后顺序\n",
    "\n",
    "> 2. 关键字参数\n",
    ">> 1. 在\n",
    "> 3. 默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81432efb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(pet_name,animal_typle):\n",
    "    \"\"\"\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf1772fe",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c11f3e2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e787827",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
